package com.ilink.groundservice.controller;/**
 * <h3>iLink-java-backend-zhangbo2</h3>
 *
 * @Description <p>作品集前端交互</p>
 * @Author ZhangBo
 * @Date 2024-11-05 11:00
 **/

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ilink.groundservice.entity.VO.culture.ArtworkResponse;
import com.ilink.groundservice.service.culture.ArtworkService;
import com.ilink.ilinkapi.dto.ground.culture.ArtworkMetadataRequest;
import com.ilink.ilinkapi.dto.ground.culture.ArtworkRequest;
import com.ilink.ilinkcommon.domain.R;
import com.ilink.ilinkcommon.log.method.MethodLog;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName ArtworkController
 * @Description TODO
 * @Author ZhangBo
 * @Date 2024-11-05 11:00
 **/
@Slf4j
@RequestMapping("/ground/culture")
@RestController
@RequiredArgsConstructor
public class ArtworkController {
    private final ArtworkService artworkService;

    /**
     * @Author ZhangBo
     * @Description 分页返回作品元数据接口
     * @Date 15:20 2024/11/5
     * @Param [request]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog("分页返回作品元数据")
    @PostMapping("/get_artworks")
    public R getArtworks(@RequestBody ArtworkMetadataRequest request) {
        try {
            IPage<ArtworkResponse> responseIPage = artworkService.getArtworkMetadata(request);
            if (responseIPage != null) {
                return R.ok("分页返回作品元数据", responseIPage);
            } else {
                return R.error("未查询到作品元数据");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * @Author ZhangBo
     * @Description 批量上传作品
     * @Date 22:18 2024/11/5
     * @Param [artworkRequests, files]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog("批量上传作品")
    @PostMapping("/upload_artworks")
    public R uploadArtworks() {
        try {
            // 调用服务层方法，处理文件上传
            return artworkService.uploadArtworksFromLocalFile();
        } catch (Exception e) {
            log.error("批量上传作品失败: " + e.getMessage());
            return R.error("批量上传作品失败: " + e.getMessage());
        }
    }

    /**
     * @Author ZhangBo
     * @Description 作品点赞
     * @Date 10:10 2024/11/6
     * @Param [jsonString]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog("作品点赞")
    @PostMapping("/add_artworks_like")
    public R addArtworksLike(@RequestBody String jsonString){
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            String artworkId = jsonObject.getString("artworkId");

            boolean like = artworkService.addArtworksLike(Long.valueOf(artworkId));
            if(like){
                return R.ok("点赞量+1");
            }else{
                return R.error("未查询到文化作品信息");
            }
        }catch (Exception e){
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * @Author ZhangBo
     * @Description 获取最常见的作品标签，最多返回8个
     * @Date 10:17 2024/11/6
     * @Param []
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog("获取最常见的作品标签，最多返回8个")
    @GetMapping("/get_common_artworks_tags")
    public R getCommonArtworksTags(){
        try {
            int num = 8;
            return R.ok(artworkService.getCommonTags(num));
        } catch(Exception e){
            return R.error("错误：" + e);
        }
    }
}
